package io.github.talelin.latticy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.bo.BannerWithItemBO;
import io.github.talelin.latticy.dto.BannerDTO;
import io.github.talelin.latticy.mapper.BannerItemMapper;
import io.github.talelin.latticy.mapper.BannerMapper;
import io.github.talelin.latticy.model.BannerDO;
import io.github.talelin.latticy.model.BannerItemDO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, BannerDO> {

    @Autowired
    private BannerMapper bannerMapper;

    @Autowired
    private BannerItemMapper bannerItemMapper;


    public BannerWithItemBO getWithItems(Long id) {
        BannerDO banner = this.getById(id);
        if (banner == null) {
            throw new NotFoundException(20000);
        }
//        QueryWrapper<BannerItemDO> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(BannerItemDO::getBannerId, id);
        //        queryWrapper.eq("banner_id", id);
//        LambdaQueryWrapper<BannerItemDO> lambda = queryWrapper.lambda();
//        LambdaQueryWrapper<BannerItemDO> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(BannerItemDO::getBannerId, id);
//        List<BannerItemDO> bannerItemDOList = bannerItemMapper.selectList(wrapper);
        // 链式调用
         List<BannerItemDO> bannerItemDOList = new LambdaQueryChainWrapper<>(bannerItemMapper)
                 .eq(BannerItemDO::getBannerId, id)
                 .list();

        return new BannerWithItemBO(banner, bannerItemDOList);
    }

    public void deleteBanner(Long id) {
        BannerDO banner = this.getById(id);
        if (banner == null) {
            throw new NotFoundException(20000);
        }
        this.getBaseMapper().deleteById(id);

    }


    public void update(BannerDTO bannerDTO, Long id) {
        BannerDO banner = this.getById(id);
        if (banner == null) {
            throw new NotFoundException(20000);
        }
        BeanUtils.copyProperties(bannerDTO, banner);
        this.updateById(banner);

    }

    public void getBanners() {
//        return bannerMapper.selectPage();
    }


}
